﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PokeTD
{
    class DashboardButton
    {
        public event DashboardButtonEventHandler DashboardButtonClicked;

        private string _Caption;
        private bool _isActive;
        private Rectangle _position;
        private SpriteFont _font;
        private Texture2D _hoverTexture;
        private Texture2D _normalTexture;
        private Texture2D _inactiveTexture;
        private Texture2D _pressedTexture;
        private Color _textColor;
        private DashboardButtonState _state;
        public enum DashboardButtonState { normal, pressed, hover };
        private MouseState lastState = Mouse.GetState();

        public DashboardButton(string capt, bool act, Rectangle pos)
        {
            _Caption = capt;
            _isActive = act;
            _position = pos;
        }

        public void Click()
        {
            DashboardButtonClicked(this, new DashboardButtonEventArgs());
        }


        public string Caption {
            get { return _Caption; }
            set { _Caption = value; }
        }
        public bool isActive {
            get { return _isActive; }
            set { _isActive = value; }
        }
        public Rectangle Position {
            get { return _position; }
            set { _position = value; }
        }
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }
        public Texture2D normalTexture
        {
            get { return _normalTexture; }
            set { _normalTexture = value; }
        }
        public Texture2D hoverTexture
        {
            get { return _hoverTexture; }
            set { _hoverTexture = value; }
        }
        public Texture2D inactiveTexture
        {
            get { return _inactiveTexture; }
            set { _inactiveTexture = value; }
        }
        public Texture2D pressedTexture
        {
            get { return _pressedTexture; }
            set { _pressedTexture = value; }
        }
        public Color textColor
        {
            get { return _textColor; }
            set { _textColor = value; }
        }
        


        public void Update()
        {
            if (_font == null || _position == null)
            {
                _isActive = false;
            }
            if (isActive)
            {
                MouseState state = Mouse.GetState();
                if (_position.Contains(new Point(state.X, state.Y)))
                {
                    _state = DashboardButtonState.hover;
                }
                else
                {
                    _state = DashboardButtonState.normal;
                }
                if (state.LeftButton == ButtonState.Pressed && _position.Contains(new Point(state.X, state.Y))) {
                    _state = DashboardButtonState.pressed;
                }
                if (lastState.LeftButton == ButtonState.Pressed && state.LeftButton == ButtonState.Released && (_position.Contains(new Point(state.X, state.Y)))) {
                    //Click'ed.
                    DashboardButtonClicked(this, new DashboardButtonEventArgs());
                }
                lastState = Mouse.GetState();
            }
        }

        public void Draw(SpriteBatch spritebatch)
        {
            if (_isActive)
            {
                if (_state == DashboardButtonState.normal)
                {
                    spritebatch.Draw(_normalTexture, _position, Color.White);
                }
                if (_state == DashboardButtonState.pressed)
                {
                    spritebatch.Draw(_pressedTexture, _position, Color.White);
                }
                if (_state == DashboardButtonState.hover)
                {
                    spritebatch.Draw(_hoverTexture, _position, Color.White);
                }
            }
            else
            {
                spritebatch.Draw(_inactiveTexture, _position, Color.White);
            }
            spritebatch.DrawString(_font, _Caption, new Vector2(_position.X + 5, _position.Y), _textColor);
        }
    }
}
